package cn.jbolt.admin.htmleditor.upload;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.httpmethod.JBoltHttpPost;
import cn.jbolt.core.base.JBoltMsg;
import cn.jbolt.core.controller.base.JBoltFormData;
import cn.jbolt.core.model.base.JBoltBaseModel;
import cn.jbolt.core.para.JBoltNoUrlPara;
import cn.jbolt.core.permission.UnCheck;
import cn.jbolt.core.service.JBoltFileService;
import cn.jbolt.core.service.base.JBoltBaseService;
import cn.jbolt.core.util.JBoltDateUtil;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.upload.UploadFile;

@UnCheck
@Path("/api/admin/jboltFile/upload")
public class JBoltFileUploadAdminController extends JBoltApiBaseController {
    @Inject
    JBoltFileService service;

    @Override
    protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
        return service;
    }

    /**
     * 上传接口 返回JboltFile对象
     */
    @JBoltHttpPost
    @Before({Tx.class,JBoltNoUrlPara.class})
    public void index(@Para("path") String path, @Para("linkId") Long linkId) {
        if (notOk(path)) {
            renderJBoltApiFail("path参数异常:不能为空");
            return;
        }
        if (FileUtil.isAbsolutePath(path) || path.startsWith(".") || path.startsWith("/") || path.contains(":") || path.contains("\\")) {
            renderJBoltApiFail("path参数异常 例:avatar/{date(yyMMddHH)}/{random(5)}/{ori}");
            return;
        }
        String fileSuffix = FileUtil.getSuffix(path);
        if (notOk(fileSuffix)) {
            renderJBoltApiFail("path参数异常 末尾未指定文件名 例:avatar/{date(yyMMddHH)}/{random(5)}/{ori}");
            return;
        }
        String savePath = path.substring(0, path.lastIndexOf("/"));
        String fileName = FileNameUtil.getName(path);
        UploadFile uploadFile = getFile("file", savePath);
        renderJBoltApiRet(service.saveFile(uploadFile, savePath, fileName,linkId));
    }

    @JBoltHttpPost
    public void progressFile(){
        UploadFile progressFile = getProgressFile("file","test", uploadProgress -> {
            System.out.println(JBoltDateUtil.getNowStr()+" 已上传 进度："+ uploadProgress.getBytesRead() +" / " + uploadProgress.getContentLength());
        });
        renderJBoltApiSuccessWithData(progressFile);
    }


    @JBoltHttpPost
    @Before({Tx.class,JBoltNoUrlPara.class})
    public void formData() throws Exception {
        JBoltFormData formData = getJBoltFormData();
        if(formData == null || formData.notOk()){
            renderJBoltApiFail(JBoltMsg.PARAM_ERROR);
            return;
        }
        UploadFile uploadFile = formData.getUploadFile();
        Long linkId = formData.getLong("linkId");
        renderJBoltApiRet(service.saveFile(formData.getUploadFile(), uploadFile.getUploadPath(), uploadFile.getFileName(),linkId));
    }
}
